package com.lzx.applib.adapter;

import android.support.v4.view.PagerAdapter;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;

/**
 * Created by Lzx on 16/11/02 0002.
 */

public abstract class BPagerAdapter<T, E extends RecyclerView.ViewHolder> extends PagerAdapter {

    private RecyclerView.Adapter<E> recyclerAdapter = null;

    public abstract E onCreateViewHolder(ViewGroup parent, int viewType);

    public abstract void onBindViewHolder(E holder, int position);

    public abstract int getItemCount();

    public abstract T getItem(int position);

    @Override
    public void notifyDataSetChanged() {
        super.notifyDataSetChanged();
        if (recyclerAdapter != null) {
            recyclerAdapter.notifyDataSetChanged();
        }
    }

    public final long getItemId(int position) {
        return position;
    }

    public int getViewTypeCount() {
        return 1;
    }

    public int getItemViewType(int position) {
        return 0;
    }

    @Override
    public final int getCount() {
        return getItemCount();
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        E holder = onCreateViewHolder(container, getItemViewType(position));
        holder.itemView.setTag(holder);
        onBindViewHolder(holder, position);
        container.addView(holder.itemView);
        return holder.itemView;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View view = (View) object;
        if (container != null) {
            container.removeView(view);
        }
    }

    public RecyclerView.Adapter<E> toRecyclerAdapter() {
        if (recyclerAdapter == null) {
            recyclerAdapter = new RecyclerView.Adapter<E>(){
                @Override
                public void onViewRecycled(E holder) {
                    BPagerAdapter.this.destroyItem(null, (int)holder.getItemId(), holder.itemView);
                }

                @Override
                public long getItemId(int position) {
                    return BPagerAdapter.this.getItemId(position);
                }

                @Override
                public int getItemViewType(int position) {
                    return BPagerAdapter.this.getItemViewType(position);
                }

                @Override
                public E onCreateViewHolder(ViewGroup parent, int viewType) {
                    return BPagerAdapter.this.onCreateViewHolder(parent, viewType);
                }

                @Override
                public void onBindViewHolder(E holder, int position) {
                    BPagerAdapter.this.onBindViewHolder(holder, position);
                }

                @Override
                public int getItemCount() {
                    return BPagerAdapter.this.getItemCount();
                }
            };
        }
        return recyclerAdapter;
    }
}
